$(document).ready(function (){
	view_init();
	event_init();
});

var url_param_id = getQueryString("id");

function view_init()
{
	loadingStart();
	$("[data-type=dateAll]").each(function (){
		$(this).datetimepicker({
			  timeFormat: "HH:mm:ss",
	          dateFormat: "yy-mm-dd",
			  monthNames: [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ]
		});
	});
 	
 	if (url_param_id)
 	{
 		um_ajax_post(
 				"/returnorder/selectExpress.do",
 				{"orderNo":url_param_id},
 				function (data){
 					if (data && data.returnorder)
 					{
 	 					$("#main-content").umDataBind("render" ,data.returnorder[0] ,"");
 	 					var temp = statusChange(data.returnorder[0].status);
 	 					$("[data-id=statusdisplay]").text(temp);
 					}

 				},
 				null,false);
 	}
 	
	loadingEnd();
	
	list_list(true);
	
	$("#main-content").show();
}

function event_init()
{
    $("#edit").click(function (){
    	returnorder_edit();
    });
    
    $("#print").click(function (){
    	returnorder_print();
    });
    
	// 返回按钮
	$("#back").click(function (){
		window.location.href = "/returnorder/direction/indexexpress.do";
	});
	
}

function returnorder_edit()
{
	if (!$("#main-content").validate())
	{
		return false;
	}
	var obj = $("#main-content").umDataBind("serialize" ,"");
	var url = "/returnorder/updateExpressMessage.do";
	obj.orderNo = url_param_id;
	
	um_ajax_post(
			url,
			obj,
			function (data){
				window.location.href = "/returnorder/direction/indexexpress.do?flag=" + SUCCESS;
			},
			null);
}

function returnorder_print()
{
	var oPop = window.open('','oPop');  
    var str = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'
    	str +='<script type="text/javascript" src="/carfee/scripts/init_css.js"></script>'
    	str +='<link rel="stylesheet" type="text/css" href="/carfee/plugin/upload/css/uploader.css" rel="stylesheet" />'
    	str +='<link rel="stylesheet" type="text/css" href="/carfee/plugin/upload/css/demo.css" rel="stylesheet" />'
    	str +='<link rel="stylesheet" type="text/css" href="/carfee/plugin/jqueryui/jquery-ui.css">'
    	str +='<link rel="stylesheet" type="text/css" href="/carfee/plugin/datePicker/jquery-ui-timepicker-addon.min.css">'
        str += document.all.item("div_print").innerHTML; 
    oPop.document.write(str); 
    oPop.document.close();
    oPop.print();  
    oPop.close();  
}


function list_list(isFirst)
{
	if (isFirst)
	{
		index_pageObj.init();
	}
	
	loadingStart();
	
	var obj = $("#query_panel").umDataBind("serialize" ,"");
	obj.currentPage = index_pageObj.currentPage;
	obj.pageSize = index_pageObj.pageSize;
	obj.orderNo = url_param_id;
	um_ajax_post(
			INDEX_WEBAPP + "/returnorder/books/select.do",
			obj,
			function (data){
				$("#page_record_count").text(data.size);
				_render(data.books);
			},
			null);
	function _render(data)
	{
		var buffer = [];
		if (data && data.length >0)
		{
			for (var i=0;i<data.length;i++)
			{
				buffer.push('<tr>');
				buffer.push('<td>'+data[i].orderNo+'</td>');
				buffer.push('<td>'+data[i].barCode+'</td>');
				buffer.push('<td>'+data[i].bookrecno+'</td>');
				buffer.push('<td>'+data[i].bookTitle+'</td>');
				buffer.push('<tr>');
			}
			$("#list_tbody").html(buffer.join(""));
		}
		else
		{
			index_pageObj.toEndPage();
		}

		index_pageObj.isEndMethod(data);

	}
}

function statusChange(str)
{
	if (str == null || str == undefined)
	{
		return "";
	}
	else if (str == "102")
	{
		// 还书单快递员更新了快递信息
		return "快递更新";
	}
	else if (str == "997")
	{
		// 借书单确认初始状态（待审核订单）：998
		return "发起还书";
	}
	else if (str == "996")
	{
		// 借书单确认初始状态（待审核订单）：998
		return "完成还书";
	}
	return str;
}